
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"
#include "DCDMColoursPage.h"
#include "../client/SettingsManager.h"
#include "../client/SyntaxManager.h"
#include "WinUtil.h"
#include "KeywordDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


PropPage::TextItem DCDMColoursPage::texts[] = {
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item DCDMColoursPage::items[] = {
	{ IDC_BACKGROUND_IMAGE, SettingsManager::BACKGROUND_IMAGE, PropPage::T_STR_SHOWDEFAULT },
	{ 0, 0, PropPage::T_END }
};

LRESULT DCDMColoursPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	//PropPage::translate((HWND)(*this), texts);
	PropPage::read((HWND)*this, items);
	WinUtil::decodeFont(SETTING(SCRIPT_FONT), scriptFont);
	//Sulan start svn94

	//Text colours
	normalColour = SETTING(NORMAL_COLOUR);
	clientCheckedNotFilelistColour = SETTING(CLIENT_CHECKED_NOT_FILELIST_COLOUR);
	fileListCheckedNotClientColour = SETTING(FILELIST_CHECKED_NOT_CLIENT_COLOUR);
	fileListAndClientCheckedColour = SETTING(FILELIST_AND_CLIENT_CHECKED_COLOUR);
	badClientColour = SETTING(BAD_CLIENT_COLOUR);
	badFilelistColour = SETTING(BAD_FILELIST_COLOUR);
	protectedColour = SETTING(PROTECTED_COLOUR);

	//Background colours
	normalColour_bg = SETTING(NORMAL_COLOUR_BG);
	clientCheckedNotFilelistColour_bg = SETTING(CLIENT_CHECKED_NOT_FILELIST_COLOUR_BG);
	fileListCheckedNotClientColour_bg = SETTING(FILELIST_CHECKED_NOT_CLIENT_COLOUR_BG);
	fileListAndClientCheckedColour_bg = SETTING(FILELIST_AND_CLIENT_CHECKED_COLOUR_BG);
	badClientColour_bg = SETTING(BAD_CLIENT_COLOUR_BG);
	badFilelistColour_bg = SETTING(BAD_FILELIST_COLOUR_BG);
	protectedColour_bg = SETTING(PROTECTED_COLOUR_BG);

	CRect rc;
	ctrlList.Attach(GetDlgItem(IDC_LIST));
	ctrlList.GetClientRect(rc);
	ctrlList.InsertColumn(0, CTSTRING(USER_LIST_COLOURS), LVCFMT_LEFT, (rc.Width()), 0); 
	//Damn this is ugly, but we need 2 hidden cols for the custom draw to be albe to read the values...
	/****/
	ctrlList.InsertColumn(1, _T(""), LVCFMT_LEFT, 0, 1);
	ctrlList.InsertColumn(2, _T(""), LVCFMT_LEFT, 0, 2);
	/****/
	if(BOOLSETTING(FULL_ROW_SELECT)){
		ctrlList.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);
	}
	doList();
	//End

	scriptNormal = SETTING(SCRIPT_NORMAL_COLOUR);
	keyword = SETTING(KEYWORD_COLOUR);
	quote = SETTING(QUOTE_COLOUR);
	comment = SETTING(COMMENT_COLOUR);
	library = SETTING(LIBRARY_COLOUR);
	operatorCol = SETTING(OPERATOR_COLOUR);
	scriptBkGrnd = SETTING(SCRIPT_BACKGROUND_COLOUR);

	m_lsbListScript.Attach( GetDlgItem(IDC_TEXT_STYLES_SCRIPT) );
	m_PreviewScript.SubclassWindow(GetDlgItem(IDC_PREVIEW_SCRIPT));
	m_PreviewScript.ClearKeywords();
	m_PreviewScript.ClearConstants();
	m_PreviewScript.ClearOperators();

	Keywords::List& kl = SyntaxManager::getInstance()->getKeywords();
	for(Keywords::Iter i = kl.begin(); i != kl.end(); ++i) {
		switch(i->getType()) {
			case _KEYWORD: m_PreviewScript.AddKeyword(i->getKeyword().c_str()); break;
			case _LIBRARY: m_PreviewScript.AddConstant(i->getKeyword().c_str()); break;
			case _OPERATOR: m_PreviewScript.AddOperator(i->getKeyword()[0]); break;
			default: dcassert(0);
		}
	}

	m_PreviewScript.SetSyntax(SC_COMMENT, _T("--"));
	m_PreviewScript.SetWindowText(_T("-- Load a file\r\n")
		_T("function pickle._load( filename )\r\n")
		_T("	local f,e = io.open( filename, \"rb\" )\r\n")
		_T("	if f then\r\n")
		_T("		local r = f:read( \"*a\" )\r\n")
		_T("		f:close()\r\n")
		_T("		return r\r\n")
		_T("	else\r\n")
		_T("		return nil,\"pickle._load: failed: \"..e\r\n")
		_T("	end\r\n")
		_T("end\r\n"));

	m_lsbListScript.AddString(CTSTRING(NORMAL));
	m_lsbListScript.AddString(CTSTRING(KEYWORDDLG_KEYWORD));
	m_lsbListScript.AddString(CTSTRING(COLORPAGE_QUOTE));
	m_lsbListScript.AddString(CTSTRING(COMMENT));
	m_lsbListScript.AddString(CTSTRING(KEYWORDDLG_LIBRARY));
	m_lsbListScript.AddString(CTSTRING(KEYWORDDLG_OPERATOR));
	refreshPreviewScript();

	CComboBox cImages;
	cImages.Attach(GetDlgItem(IDC_USER_LIST_IMAGES));
	cImages.AddString(CTSTRING(DCPP));
	cImages.AddString(CTSTRING(ODC));
	cImages.AddString(CTSTRING(CZDCPP));
	cImages.SetCurSel(SETTING(USER_LIST_IMAGES));
	cImages.Detach();

	return TRUE;
}
LRESULT DCDMColoursPage::onChangeColour(WORD /*wNotifyCode*/, WORD wID, HWND hWndCtl, BOOL& /*bHandled*/) {
	//Sulan start svn94
	int index = ctrlList.GetNextItem(-1, LVNI_SELECTED);
	if(ctrlList.GetSelectedCount() != 1) return 0;
	int colour = 0;
	switch(wID) {
		case IDC_NORMAL_COLOUR:
		{
			switch(index) {
				case 0: colour = normalColour; break;
				case 1: colour = clientCheckedNotFilelistColour; break;
				case 2: colour = fileListCheckedNotClientColour; break;
				case 3: colour = fileListAndClientCheckedColour; break;
				case 4: colour = badClientColour; break;
				case 5: colour = badFilelistColour; break;
				case 6: colour = protectedColour; break;
			default: break;
			}
			CColorDialog d( colour, 0, hWndCtl );
			if (d.DoModal() == IDOK) {
				switch(index) {
					case 0: normalColour = d.GetColor(); break;
					case 1: clientCheckedNotFilelistColour = d.GetColor(); break;
					case 2: fileListCheckedNotClientColour = d.GetColor(); break;
					case 3: fileListAndClientCheckedColour = d.GetColor(); break;
					case 4: badClientColour = d.GetColor(); break;
					case 5: badFilelistColour = d.GetColor(); break;
					case 6: protectedColour = d.GetColor(); break;
					default: break;
				}
			}
		}break;
		case IDC_BG_COLOUR:
		{
			switch(index) {
				case 0: colour = normalColour_bg; break;
				case 1: colour = clientCheckedNotFilelistColour_bg; break;
				case 2: colour = fileListCheckedNotClientColour_bg; break;
				case 3: colour = fileListAndClientCheckedColour_bg; break;
				case 4: colour = badClientColour_bg; break;
				case 5: colour = badFilelistColour_bg; break;
				case 6: colour = protectedColour_bg; break;
			default: break;
			}
			CColorDialog d( colour, 0, hWndCtl );
			if (d.DoModal() == IDOK) {
				switch(index) {
					case 0: normalColour_bg = d.GetColor(); break;
					case 1: clientCheckedNotFilelistColour_bg = d.GetColor(); break;
					case 2: fileListCheckedNotClientColour_bg = d.GetColor(); break;
					case 3: fileListAndClientCheckedColour_bg = d.GetColor(); break;
					case 4: badClientColour_bg = d.GetColor(); break;
					case 5: badFilelistColour_bg = d.GetColor(); break;
					case 6: protectedColour_bg = d.GetColor(); break;
					default: break;
				}
			}
		}break;
	}
	ctrlList.DeleteAllItems();
	doList();
	return TRUE;
	//End
}


LRESULT DCDMColoursPage::onChangeColourScript(WORD /*wNotifyCode*/, WORD /*wID*/, HWND hWndCtl, BOOL& /*bHandled*/) {
	int index = m_lsbListScript.GetCurSel();
	if(index == -1) return 0;
	int colour = 0;
	switch(index) {
		case 0: colour = scriptNormal; break;
		case 1: colour = keyword; break;
		case 2: colour = quote; break;
		case 3: colour = comment; break;
		case 4: colour = library; break;
		case 5: colour = operatorCol; break;
		default: break;
	}
	CColorDialog d( colour, 0, hWndCtl );
	if (d.DoModal() == IDOK) {
		switch(index) {
			case 0: scriptNormal = d.GetColor();	break;
			case 1: keyword = d.GetColor();	break;
			case 2: quote = d.GetColor();	break;
			case 3: comment = d.GetColor();	break;
			case 4: library = d.GetColor();	break;
			case 5: operatorCol = d.GetColor();	break;
			default: break;
		}
		refreshPreviewScript();
	}
	return TRUE;
}

void DCDMColoursPage::refreshPreviewScript() {
	m_PreviewScript.SetBackgroundColor(scriptBkGrnd);
	m_PreviewScript.SetFont(::CreateFontIndirect(&scriptFont));

	SYNTAXCOLOR ic = { 0 };
	ic.clrText = scriptNormal;
	m_PreviewScript.SetSyntaxColor(SC_NORMAL, ic);
	ic.clrText = library;
	m_PreviewScript.SetSyntaxColor(SC_LIBRARY, ic);
	ic.clrText = operatorCol;
	m_PreviewScript.SetSyntaxColor(SC_OPERATOR, ic);
	ic.clrText = keyword;
	m_PreviewScript.SetSyntaxColor(SC_KEYWORD, ic);
	ic.clrText = comment;
	m_PreviewScript.SetSyntaxColor(SC_COMMENT, ic);
	ic.clrText = quote;
	m_PreviewScript.SetSyntaxColor(SC_STRING, ic);

	m_PreviewScript.FormatAll();
	m_PreviewScript.SetSel(0, 0);

	m_PreviewScript.InvalidateRect(NULL);
}

LRESULT DCDMColoursPage::onChangeFontScript(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	LOGFONT tmp = scriptFont;
	CFontDialog d(&tmp, CF_SCREENFONTS, NULL, *this);
	if(d.DoModal() == IDOK) {
		scriptFont = tmp;
		refreshPreviewScript();
	}
	return TRUE;
}

LRESULT DCDMColoursPage::onChangeBackground(WORD /*wNotifyCode*/, WORD /*wID*/, HWND hWndCtl, BOOL& /*bHandled*/) {
	CColorDialog d( scriptBkGrnd, 0, hWndCtl );
	if (d.DoModal() == IDOK) {
		scriptBkGrnd = d.GetColor();
		refreshPreviewScript();
	}
	return TRUE;
}
LRESULT DCDMColoursPage::onKeywordDialog(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	KeywordDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT DCDMColoursPage::onBrowseImage(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	TCHAR buf[MAX_PATH];
	string ap = Util::getAppPath();

	GetDlgItemText(IDC_BACKGROUND_IMAGE, buf, MAX_PATH);
	tstring x = buf;
	if(x.find(':') == string::npos) {
		x = ap + x;
	}

	if(WinUtil::browseFile(x, m_hWnd, false) == IDOK) {
		if(Util::strnicmp(x.c_str(), ap.c_str(), ap.size()) == 0) {
			x.replace(0, ap.size(), _T(""));
		}
		SetDlgItemText(IDC_BACKGROUND_IMAGE, x.c_str());
	}
	return 0;
}

void DCDMColoursPage::write() {
	PropPage::write((HWND)*this, items);
	//Sulans start
	//Text colours
	settings->set(SettingsManager::NORMAL_COLOUR, normalColour);
	settings->set(SettingsManager::CLIENT_CHECKED_NOT_FILELIST_COLOUR, clientCheckedNotFilelistColour);
	settings->set(SettingsManager::FILELIST_CHECKED_NOT_CLIENT_COLOUR, fileListCheckedNotClientColour);
	settings->set(SettingsManager::FILELIST_AND_CLIENT_CHECKED_COLOUR, fileListAndClientCheckedColour);
	settings->set(SettingsManager::BAD_CLIENT_COLOUR, badClientColour);
	settings->set(SettingsManager::BAD_FILELIST_COLOUR, badFilelistColour);
	settings->set(SettingsManager::PROTECTED_COLOUR, protectedColour);
	//Sulans start svn94
	//Background colours
	settings->set(SettingsManager::NORMAL_COLOUR_BG, normalColour_bg);
	settings->set(SettingsManager::CLIENT_CHECKED_NOT_FILELIST_COLOUR_BG, clientCheckedNotFilelistColour_bg);
	settings->set(SettingsManager::FILELIST_CHECKED_NOT_CLIENT_COLOUR_BG, fileListCheckedNotClientColour_bg);
	settings->set(SettingsManager::FILELIST_AND_CLIENT_CHECKED_COLOUR_BG, fileListAndClientCheckedColour_bg);
	settings->set(SettingsManager::BAD_CLIENT_COLOUR_BG, badClientColour_bg);
	settings->set(SettingsManager::BAD_FILELIST_COLOUR_BG, badFilelistColour_bg);
	settings->set(SettingsManager::PROTECTED_COLOUR_BG, protectedColour_bg);
	//End
	settings->set(SettingsManager::KEYWORD_COLOUR, keyword);
	settings->set(SettingsManager::QUOTE_COLOUR, quote);
	settings->set(SettingsManager::COMMENT_COLOUR, comment);
	settings->set(SettingsManager::SCRIPT_NORMAL_COLOUR, scriptNormal);
	settings->set(SettingsManager::LIBRARY_COLOUR, library);
	settings->set(SettingsManager::OPERATOR_COLOUR, operatorCol);
	string f = WinUtil::encodeFont(scriptFont);
	settings->set(SettingsManager::SCRIPT_FONT, f);
	settings->set(SettingsManager::SCRIPT_BACKGROUND_COLOUR, scriptBkGrnd);

	CComboBox cImages(GetDlgItem(IDC_USER_LIST_IMAGES));
	settings->set(SettingsManager::USER_LIST_IMAGES, cImages.GetCurSel());

	// We need to detach all objects
	cImages.Detach();
	ctrlList.Detach();
	m_lsbListScript.Detach();
	m_PreviewScript.Detach();
}

//Sulan start svn94
void DCDMColoursPage::doList(){
	//@Fixme set customable background colour
	insertItem(CTSTRING(NORMAL),							normalColour,					normalColour_bg);
	insertItem(CTSTRING(COLORPAGE_CLIENT_NOT_FILELIST),	clientCheckedNotFilelistColour, clientCheckedNotFilelistColour_bg);
	insertItem(CTSTRING(COLORPAGE_FILELIST_NOT_CLIENT),	fileListCheckedNotClientColour, fileListCheckedNotClientColour_bg);
	insertItem(CTSTRING(COLORPAGE_FILELIST_AND_CLIENT),	fileListAndClientCheckedColour, fileListAndClientCheckedColour_bg);
	insertItem(CTSTRING(COLORPAGE_BADCLIENT),			badClientColour,				badClientColour_bg);
	insertItem(CTSTRING(COLORPAGE_BADFILELIST),			badFilelistColour,				badFilelistColour_bg);
	insertItem(CTSTRING(COLORPAGE_PROTECTED),			protectedColour,				protectedColour_bg);
}

LRESULT DCDMColoursPage::onCustomDraw(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) {
	LPNMLVCUSTOMDRAW cd = (LPNMLVCUSTOMDRAW)pnmh;

	switch(cd->nmcd.dwDrawStage) {
	case CDDS_PREPAINT:
		return CDRF_NOTIFYITEMDRAW;

	case CDDS_ITEMPREPAINT:
		{
			try	{
				TCHAR buf[256];
				//Text colour
				ctrlList.GetItemText(cd->nmcd.dwItemSpec, 1, buf, 256);
				cd->clrText = Util::toInt(buf);
				//Background colour
				ctrlList.GetItemText(cd->nmcd.dwItemSpec, 2, buf, 256);
				if(Util::toInt(buf) < 0)
					cd->clrTextBk = WinUtil::bgColor;
				else
					cd->clrTextBk = Util::toInt(buf);


				return CDRF_NEWFONT | CDRF_NOTIFYSUBITEMDRAW;
			}
			catch(const Exception&)
			{
			}
			catch(...) 
			{
			}
		}
		return CDRF_NOTIFYSUBITEMDRAW;

	default:
		return CDRF_DODEFAULT;
	}
}
//End
/**
 * @file
 * $Id: DCDMColoursPage.cpp 259 2005-07-16 23:47:39Z Pothead $
 */